package com.example.aacwebdemo.es.core.bean;


import org.elasticsearch.search.sort.SortOrder;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhao.xin06679
 * @date 2021年06月22日 17:35
 */
public class ESSort {

    public final List<ESOrder> orders;

    public List<ESOrder> getOrders() {
        return orders;
    }

    public ESSort() {
        orders = new ArrayList<>();
    }

    public ESSort(SortOrder direction, String property) {
        orders = new ArrayList<>();
        add(direction,property);
    }

    /**
     * 追加排序字段
     * @param direction  排序方向
     * @param property  排序字段
     * @return
     */
    public ESSort add(SortOrder direction, String property) {

        Assert.notNull(direction, "direction must not be null!");
        Assert.hasText(property, "fieldName must not be empty!");

        orders.add(ESOrder.builder().direction(direction).property(property).build());
        return this;
    }

    public static class  ESOrder implements Serializable {

        private final SortOrder directionA;
        private final String property;

        public SortOrder getDirection() {
            return directionA;
        }

        public String getProperty() {
            return property;
        }


        public ESOrder (SortOrder direction,String property){
            this.directionA = direction;
            this.property = property;
        }

        public static Builder builder(){
            return new Builder();
        }

        public static class Builder{

            private SortOrder direction;
            private String property;

            public Builder direction(SortOrder direction1){
                this.direction=direction1;
                return this;
            }

            public Builder property(String property1){
                this.property=property1;
                return this;
            }

            public ESOrder build(){
                return new ESOrder(direction,property);
            }
        }
    }

}
